<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>layui</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
        <link rel="stylesheet" href="/css/public.css" media="all">
        <style>
            body {
                background-color: #ffffff;
            }
        </style>
    </head>
    <body>
        <div class="layui-form layuimini-form" lay-filter="userForm" >
            <div class="layui-form-item">
                <label class="layui-form-label required">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="menuName" lay-verify="required" lay-reqtext="菜单名称不能为空" placeholder="菜单名称" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">链接</label>
                <div class="layui-input-block">
                    <input type="text" name="menuLink" lay-verify="required" lay-reqtext="菜单连接不能为空" placeholder="菜单连接" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">权限</label>
                <div class="layui-input-block">
                    <input type="text" name="menuAuthority" lay-verify="required" lay-reqtext="权限标识不能为空" placeholder="权限标识" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">图标</label>
                <div class="layui-input-block">
                    <input type="text" name="menuIco" lay-verify="required" lay-reqtext="菜单图标不能为空" placeholder="菜单图标" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">上级菜单</label>
                <div class="layui-input-block">
                    <input type="text" name="parentName" id="parentName" readonly lay-verify="required" lay-reqtext="权限不能为空" placeholder="请选择权限" value="" class="layui-input">
                    <input type="hidden" name="parentId" id="parentId">
                </div>
            </div>
            <input type="hidden" name="menuId" />

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>

                </div>
            </div>

        </div>

        <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
        <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

        <script>
            layui.use(['form','table', 'tableSelect'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    tableSelect = layui.tableSelect,
                    $ = layui.$;


                tableSelect.render({
                    elem: '#parentName',
                    checkedKey: 'parentId',
                    table: {
                        url: '/menu/jsonList',
                        cols: [ [
                            {type: 'radio' },
                            {field: 'menuName', title: '角色名称'},
                            {field: 'menuId', title: 'ID'}
                        ]]
                    },
                    done: function (elem, data) {
                        var NEWJSON = []
                        var NewAuth = []
                        layui.each(data.data, function (index, item) {
                            NEWJSON.push(item.menuName)
                            NewAuth.push(item.menuId)
                        })
                        elem.val(NEWJSON.join(","))
                        $('#parentId').val(NewAuth.join(","))
                    }
                });

                //监听提交
                form.on('submit(saveBtn)', function (data) {

                    parent.layer.load();
                    $.ajax({
                        type: "POST",
                        url: "/menu/menuAdd",
                        data: data.field,
                        async: true,
                        dataType: "json",
                        success: function(data) {
                            parent.layer.closeAll('loading');

                            if(data.code == 1) {
                                parent.layer.msg(data.data);

                                parent.layui.treeTable.reload("currentTableId");

                                // 关闭弹出层
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);

                            }
                            else
                            {
                                parent.layer.alert(data.data,{icon:2});
                            }

                        },
                        error: function(e) {
                            layer.closeAll('loading');
                            parent.layer.alert(e);
                        }
                    });

                    return false;
                });

            });
        </script>
    </body>
</html>